package com.androidhive.androidsqlite;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button button1, button2, button3, button4;
	TextView tv;
	StringBuilder data = new StringBuilder();
	LocationManager mLocationManager;
	ContentValues initialValues = new ContentValues();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		final Criteria criteria = new Criteria();

		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.ACCURACY_HIGH);

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.Button02);
		button3 = (Button) findViewById(R.id.Button03);
		button4 = (Button) findViewById(R.id.Button04);
		tv = (TextView) findViewById(R.id.textView1);
		final DatabaseHandler db = new DatabaseHandler(this);
		Log.d("Insert: ", "Inserting ..");
		button1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				String locationprovider = mLocationManager.getBestProvider(
						criteria, true);

				// Getting Location from location Manager using location
				// provider

				final Location mLocation = mLocationManager
						.getLastKnownLocation(locationprovider);
				// String Builder to store data.
				StringBuilder mSB = new StringBuilder();
				// location manager with with location change listener
				mLocationManager.requestLocationUpdates(locationprovider, 0,
						0.0f, new LocationListener() {
							// these methods are required
							public void onLocationChanged(Location location) {

							}

							public void onProviderDisabled(String arg0) {

								Toast.makeText(getBaseContext(),
										"ProviderDisabled", Toast.LENGTH_LONG)
										.show();

							}

							public void onProviderEnabled(String provider) {

								Toast.makeText(getBaseContext(),
										"Provider Enabled", Toast.LENGTH_LONG)
										.show();

							}

							public void onStatusChanged(String a, int b,
									Bundle c) {
							}
						});

				if (mLocation != null) {
					mSB.append("# LATITUDE & LONGITUDE \n");
					mSB.append("______________________________\n");

					mSB.append(mLocation.getLatitude()).append(" , ");
					initialValues.put("LATITUDE", mLocation.getLatitude());
					mSB.append(mLocation.getLongitude()).append("\n");
					initialValues.put("LONGITUDE", mLocation.getLatitude());
					// Geocoder Instance creation

					Geocoder gcd = new Geocoder(getBaseContext(), Locale
							.getDefault());

					List<Address> addresses;
					try {

						// getting list addresses from Geocoder

						addresses = gcd.getFromLocation(
								mLocation.getLatitude(),
								mLocation.getLongitude(), 1);
						if (addresses.size() > 0) {
							mSB.append("\n# ADDRESS \n");
							mSB.append("______________________________\n");

							for (int j = 0; j < addresses.size(); j++) {
								Address returnedAddress = addresses.get(j);

								for (int i = 0; i < returnedAddress
										.getMaxAddressLineIndex(); i++) {
									mSB.append(
											returnedAddress.getAddressLine(i))
											.append("\n");

									initialValues.put("LONGITUDE",
											mSB.toString());

								}

							}

						} else {
							mSB.append("address not found");
							initialValues.put("ADDRESS", mSB.toString());

						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				} else {
					mSB.append("Location can not be found");
					initialValues.put("ADDRESS", mSB.toString());

				}

				tv.setText(mSB.toString());
				Log.d("log", initialValues.toString());

			}
		});
		button2.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				List<MyLocation> contacts = db.getAllContacts();
				data = new StringBuilder();

				for (MyLocation cn : contacts) {
					String log = "Id: " + cn.getID() + " ,Name: "
							+ cn.getName() + " ,Lat: " + cn.getlat().toString()
							+ " ,Long: " + cn.getlon().toString();

					data.append(log);
					data.append("\n");

					Log.d("Name: ", log);

				}
				tv.setText("");
				tv.setText(data.toString());
			}
		});
		button3.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.deleteallContact();
			}
		});
		button4.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.addContact(new MyLocation("patel", 55.12, 20.11));
			}
		});
	}

}
